package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.AddressClient;
import com.hmall.common.client.ItemClient;
import com.hmall.common.dto.OrderDetail;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.common.pojo.Order;
import com.hmall.common.util.ThreadLocalUtil;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.OrderDto;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private AddressClient addressClient;

    /**
     * 提交订单
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long submit(OrderDto orderDto) {
        Long itemId = orderDto.getItemId();
        Integer num = orderDto.getNum();
        Integer paymentType = orderDto.getPaymentType();
        Long addressId = orderDto.getAddressId();

        // 生成订单
        // 根据itemId查看商品
        Item item = itemClient.getById(itemId);
        // 补充剩余字段
        Order order = new Order();
        order.setTotalFee(item.getPrice() * num);
        order.setPaymentType(paymentType);
        order.setUserId(ThreadLocalUtil.getCurrentId());
        order.setStatus(1);
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());

        orderMapper.insert(order);

        // 生成订单详情
        OrderDetail orderDetail = new OrderDetail().builder()
                .orderId(order.getId())
                .itemId(itemId)
                .num(num)
                .title(item.getName())
                .spec(item.getSpec())
                .price(item.getPrice() * num)
                .image(item.getImage())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        orderDetailMapper.insert(orderDetail);

        // 根据地址id查找具体信息
        Address address = addressClient.findAddressById(addressId);

        // 生成订单物流信息
        OrderLogistics orderLogistics = new OrderLogistics().builder()
                .orderId(order.getId())
                .contact(address.getContact())
                .phone(address.getMobile())
                .province(address.getProvince())
                .city(address.getCity())
                .town(address.getTown())
                .street(address.getStreet())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        orderLogisticsMapper.insert(orderLogistics);

        // 减库存
        item.setSold(item.getSold() + num);
        item.setStock(item.getStock() - num);
        item.setUpdateTime(LocalDateTime.now());
        itemClient.updateById(item);

        return order.getId();
    }


}
